package com.example.demo.leetcode.top100;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _23反转链表
 * @description
 * @date 2023/06/09 15:52
 * <p>
 * ******************************************************
 */
public class _23反转链表 {

    /**
     * 返回新链表的头节点
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        // 反转当作后续遍历
        ListNode last = reverseList(head.next);
        // 交换两个节点之间的箭头（指向）
        head.next.next = head;
        head.next = null;

        return last;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
